package com.front.web.filecommon;


import com.bootdo.common.config.BootdoConfig;
import com.bootdo.common.domain.FileDO;
import com.bootdo.common.service.FileService;
import com.bootdo.common.utils.DateUtils;
import com.bootdo.common.utils.FileType;
import com.bootdo.common.utils.FileUtil;
import com.bootdo.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.Serializable;
import java.util.Date;


@Controller
@RequestMapping("/f/file/common/")
public class FileCommon {


    @Autowired
    private BootdoConfig bootdoConfig;
     @Autowired
    private FileService sysFileService;

    @RequestMapping(value = "upload")
    @ResponseBody
    public UEResult upload(
            @RequestParam(value = "upfile",required = false) MultipartFile file,
            HttpServletRequest request , HttpServletResponse response) throws Exception {
        String fileName = file.getOriginalFilename();
        fileName = FileUtil.renameToUUID(fileName);

        String datePath  = DateUtils.format(new Date(), "yyyy/MM/dd/");
        File targetFile = new File(bootdoConfig.getUploadPath()+datePath);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }

        FileUtil.uploadFile(file.getBytes(), bootdoConfig.getUploadPath()+datePath, fileName);
        UEResult ueResult = new UEResult();
        ueResult.setTitle(fileName);
        ueResult.setOriginal(fileName);
        ueResult.setUrl(bootdoConfig.getUeditfilepath() + datePath+fileName);
        ueResult.setType(fileName.substring(fileName.lastIndexOf(".") + 1));
        ueResult.setState("SUCCESS");
        return  ueResult;
    }




    /*@RequestMapping(value = "uploadR")
    @ResponseBody
    public R uploadR(
            @RequestParam(value = "upfile",required = false) MultipartFile file,
            HttpServletRequest request , HttpServletResponse response) throws Exception {
        String fileName = file.getOriginalFilename();
        fileName = FileUtil.renameToUUID(fileName);
        String datePath  = DateUtils.format(new Date(), "yyyy/MM/dd/");
        File targetFile = new File(bootdoConfig.getUploadPath()+datePath);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        FileUtil.uploadFile(file.getBytes(), bootdoConfig.getUploadPath()+datePath, fileName);
        return  R.ok().put("name",datePath+fileName);
    }  */

    @RequestMapping(value = "uploadR")
    @ResponseBody
    public R uploadR(
            @RequestParam(value = "upfile",required = false) MultipartFile file,
            HttpServletRequest request , HttpServletResponse response) throws Exception {

        String fileName = file.getOriginalFilename();
        fileName = FileUtil.renameToUUID(fileName);

        String datePath  = DateUtils.format(new Date(), "yyyy/MM/dd/");
        File targetFile = new File(bootdoConfig.getUploadPath()+datePath);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        FileDO sysFile = new FileDO(FileType.fileType(fileName), "/files/" + datePath+fileName, new Date());
        //sysFile.setType(type);
        try {
            FileUtil.uploadFile(file.getBytes(), bootdoConfig.getUploadPath()+datePath, fileName);
        } catch (Exception e) {
            return R.error();
        }

        if (sysFileService.save(sysFile) > 0) {
            return R.ok().put("fileName",sysFile.getUrl());
        }
        return R.error();
    }


    /*@RequestMapping(value = "upload")
    public void upload(
            @RequestParam(value = "upfile",required = false) MultipartFile file,
            HttpServletRequest request , HttpServletResponse response) throws Exception {
        String fileName = file.getOriginalFilename();
        fileName = FileUtil.renameToUUID(fileName);

        String datePath  = DateUtils.format(new Date(), "yyyy/MM/dd/");
        File targetFile = new File(bootdoConfig.getUploadPath()+datePath);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }

        FileUtil.uploadFile(file.getBytes(), bootdoConfig.getUploadPath()+datePath, fileName);
        UEResult ueResult = new UEResult();
        ueResult.setTitle("zxj.png");
        ueResult.setOriginal("zxj.png");
        ueResult.setUrl("/files/" + datePath+fileName);
        ueResult.setType("png");
        ueResult.setState("SUCCESS");
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write(JSONObject.toJSONString(ueResult));
        response.getWriter().flush();
        response.getWriter().close();
    }*/

    private static class UEResult implements Serializable {
        private String title;    //这里需要设置文件名称如
        private String original;    //这里需要设置文件名称如
        private String state;    //上传状态 SUCCESS必须大写
        private String url; //图片的url
        private String code;
        private String type;

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getOriginal() {
            return original;
        }

        public void setOriginal(String original) {
            this.original = original;
        }

        public String getState() {
            return state;
        }

        public void setState(String state) {
            this.state = state;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }
    }
}
